package com.mhc.haval.controller;

import com.mhc.haval.dal.model.WorkflowDO;
import com.mhc.haval.form.WorkflowForm;
import com.mhc.haval.service.WorkFlowService;
import com.subaru.common.entity.BizResult;
import com.subaru.common.entity.ErrorCodeEnum;
import com.subaru.common.util.BeanCopierUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by kanon on 2017/9/12.
 */
@Slf4j
@RestController
@RequestMapping(value = "/workflow")
public class WorkflowController {

    @Autowired
    private WorkFlowService workFlowService;

    @ApiOperation(value = "获取流程列表", notes = "")
    @RequestMapping(value = {"/list/{companyNo}"}, method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companyNo", paramType = "path", value = "公司编号", required = true),
    })
    public BizResult<List<WorkflowDO>> getWorkflowList(@PathVariable String companyNo) {
        List<WorkflowDO> workflowDOList = workFlowService.getAllWorkflow(companyNo);
        return BizResult.create(workflowDOList, true, ErrorCodeEnum.SUCCESS.getErrCode(), ErrorCodeEnum.SUCCESS
                .getErrMsg());
    }

    @ApiOperation(value = "删除流程", notes = "")
    @RequestMapping(value = {"/{workflowId}"}, method = RequestMethod.DELETE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "workflowId", paramType = "path", value = "流程Id", required = true, dataType =
                    "Long"),
    })
    public BizResult<Boolean> deleteWorkflow(@PathVariable Long workflowId) {
        try {
            Boolean flag = workFlowService.deleteWorkflow(workflowId);
            return BizResult.create(flag, true, ErrorCodeEnum.SUCCESS.getErrCode(), ErrorCodeEnum.SUCCESS.getErrMsg());
        } catch (Exception e) {
            return BizResult.create(ErrorCodeEnum.DATA_ERROR.getErrCode(), ErrorCodeEnum.DATA_ERROR.getErrMsg());
        }
    }

    @ApiOperation(value = "获取流程", notes = "")
    @RequestMapping(value = {"/{workflowId}"}, method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "workflowId", paramType = "path", value = "流程Id", required = true, dataType =
                    "Long"),
    })
    public BizResult<WorkflowDO> getWorkflow(@PathVariable Long workflowId) {
        try {
            WorkflowDO workflowDO = workFlowService.getWorkflow(workflowId);
            return BizResult.create(workflowDO, true, ErrorCodeEnum.SUCCESS.getErrCode(), ErrorCodeEnum.SUCCESS
                    .getErrMsg());
        } catch (Exception e) {
            return BizResult.create(ErrorCodeEnum.QUERY_FAIL.getErrCode(), ErrorCodeEnum.QUERY_FAIL.getErrMsg());
        }
    }


    /**
     * 新增流程
     *
     * @param workflowForm
     * @return
     */
    @ApiOperation(value = "新增流程", notes = "")
    @RequestMapping(value = {"/"}, method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "workflowForm", dataType = "WorkflowForm", value = "流程form", required = true),
    })
    public BizResult<Boolean> addWorkflow(@RequestBody WorkflowForm workflowForm) {
        try {
            if (workflowForm.getApplyType() == null || workflowForm.getCompanyNo() == null
                    || workflowForm.getFlowName() == null || workflowForm.getFlowTempId() == null) {
                return BizResult.create(ErrorCodeEnum.INSERT_FAIL.getErrCode(), ErrorCodeEnum.INSERT_FAIL.getErrMsg());
            }
            WorkflowDO workflowDO = BeanCopierUtil.convert(workflowForm, WorkflowDO.class);
            Boolean flag = workFlowService.addWorkflow(workflowDO);
            return BizResult.create(flag, ErrorCodeEnum.SUCCESS.getErrCode(), ErrorCodeEnum.SUCCESS.getErrMsg());
        } catch (Exception e) {
            log.error("error in addWorkflow", e);
            return BizResult.create(ErrorCodeEnum.INSERT_FAIL.getErrCode(), ErrorCodeEnum.INSERT_FAIL.getErrMsg());
        }
    }

    /**
     * 修改流程
     *
     * @param workflowForm
     * @return
     */
    @ApiOperation(value = "修改流程", notes = "")
    @RequestMapping(value = {"/"}, method = RequestMethod.PUT)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "workflowForm", dataType = "WorkflowForm", value = "流程form", required = true),
    })
    public BizResult<Boolean> modifyWorkflow(@RequestBody WorkflowForm workflowForm) {
        try {
            if (workflowForm.getApplyType() == null || workflowForm.getCompanyNo() == null
                    || workflowForm.getFlowName() == null || workflowForm.getFlowTempId() == null) {
                return BizResult.create(ErrorCodeEnum.INSERT_FAIL.getErrCode(), ErrorCodeEnum.INSERT_FAIL.getErrMsg());
            }
            WorkflowDO workflowDO = BeanCopierUtil.convert(workflowForm, WorkflowDO.class);
            Boolean flag = workFlowService.modifyWorkflow(workflowDO);
            return BizResult.create(flag, ErrorCodeEnum.SUCCESS.getErrCode(), ErrorCodeEnum.SUCCESS.getErrMsg());
        } catch (Exception e) {
            log.error("error in modifyWorkflow", e);
            return BizResult.create(ErrorCodeEnum.UPDATE_FAIL.getErrCode(), ErrorCodeEnum.UPDATE_FAIL.getErrMsg());
        }
    }


}
